﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Minestorm.Evolved.Scenes;
using Microsoft.Xna.Framework.Graphics;
using Minestorm.Evolved.Assets;
using Microsoft.Xna.Framework;

namespace Minestorm.Evolved.SceneObjects
{
    public class AsteroidHologram : SceneObject
    {
        private Model _model;
        private HologramEffect _effect;

        public override void LoadContent()
        {
            var content = ObjectFactory.InstanceOf<IAssetManager>();

            _model = content.Load(Models.Asteroid);
            _effect = content.Load(Effects.Hologram);

            _effect.Projection = Scene.Camera.Projection;

            base.LoadContent();
        }

        public override void Update(GameTime gameTime)
        {
            xang += (float)gameTime.ElapsedGameTime.TotalMilliseconds * 0.0004f;

            base.Update(gameTime);
        }

        float xang = 0f;

        public override void Draw(GameTime gameTime)
        {
            _effect.View = Scene.Camera.View;

            var world = Matrix.Identity * Matrix.CreateRotationY(xang) * Matrix.CreateTranslation(Scene.Camera.LookAt);

            var boneTransforms = new Matrix[_model.Bones.Count];

            _model.CopyAbsoluteBoneTransformsTo(boneTransforms);

            foreach (var mesh in _model.Meshes)
            {
                foreach (var part in mesh.MeshParts)
                    part.Effect = _effect;

                _effect.World = boneTransforms[mesh.ParentBone.Index] * world;

                mesh.Draw();
            }

            base.Draw(gameTime);
        }
    }
}
